<template>
    <div class="content-container">
        <el-card>
            <!-- 查询部分 -->
            <el-form :model="query" :inline="true" class="demo-form-inline">
                <el-form-item label="日志标题">
                    <el-input v-model="query.title" clearable />
                </el-form-item>
                <el-form-item label="设备类型">
                    <el-input v-model="query.deviceType" clearable />
                </el-form-item>
                <el-form-item label="操作人员">
                    <el-input v-model="query.nickname" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="formQuery(false)">查询</el-button>
                    <el-button type="warning" @click="formQuery(true)">重置</el-button>
                </el-form-item>
            </el-form>
            <!-- 数据部分 -->
            <el-table max-height="500px" :data="tableData" stripe style="width: 100%; margin-top: 10px"
                ref="tableDataRef">
                <el-table-column type="selection" />
                <el-table-column prop="title" label="日志标题" />
                <el-table-column prop="clientType" label="请求来源" />
                <el-table-column prop="result" label="响应结果" />
                <el-table-column prop="cost" label="耗时(ms)" />
                <el-table-column prop="nickname" label="操作人员" />
                <el-table-column prop="tenantName" label="租户" />
                <el-table-column prop="ip" label="ip地址" />
                <el-table-column prop="createTime" label="时间" />
                <el-table-column fixed="right" label="操作">
                    <template #default="scope">
                        <el-button @click="viewDialog(scope.row)" link type="success">
                            查看
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div style="margin-top: 20px">
                <el-pagination v-model:current-page="query.current" v-model:page-size="query.size"
                    :page-sizes="[100, 200, 300, 400]" small background layout="total, sizes, prev, pager, next, jumper"
                    :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
            </div>

            <el-dialog v-model="viewVisible" width="60%" :show-close="false" :close-on-click-modal="false">
                <el-input v-model="viewData.title" disabled>
                    <template #prepend>日志标题</template>
                </el-input>
                <el-input v-model="viewData.deviceType" disabled>
                    <template #prepend>设备类型</template>
                </el-input>
                <el-input v-model="viewData.clientType" disabled>
                    <template #prepend>请求来源</template>
                </el-input>
                <el-input v-model="viewData.result" disabled>
                    <template #prepend>响应结果</template>
                </el-input>
                <el-input v-model="viewData.cost" disabled>
                    <template #prepend>耗时(ms)</template>
                </el-input>
                <el-input v-model="viewData.nickname" disabled>
                    <template #prepend>操作人员</template>
                </el-input>
                <el-input v-model="viewData.tenantName" disabled>
                    <template #prepend>租户名称</template>
                </el-input>
                <el-input v-model="viewData.ip" disabled>
                    <template #prepend>ip地址</template>
                </el-input>
                <el-input :rows="1" v-model="viewData.params" disabled>
                    <template #prepend>请求参数</template>
                </el-input>
                <el-input v-model="viewData.resp" disabled>
                    <template #prepend>返回结果</template>
                </el-input>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="viewVisible = false">关闭</el-button>
                    </span>
                </template>
            </el-dialog>
        </el-card>
    </div>
</template>

<script setup lang="ts" name="Log">
import { onMounted, ref, reactive, toRefs } from 'vue'
import type { Log, LogModDTO, LogQueryDTO, LogVo } from '@/api/system/log/type'
import { logQuery } from '@/api/system/log'

const tableDataRef = ref()
const initQuery = () => {
    return { current: 1, size: 10 } as LogQueryDTO
}
const state = reactive<any>({
    query: initQuery(),
    tableData: [] as Log[],
    total: 0,
    dialogVisible: false,
    viewVisible: false,
    viewData: {} as LogVo,
    formData: {} as LogModDTO,
    rules: {},
})
const { query, tableData, total, viewVisible, viewData } = toRefs(state)
const viewDialog = (data: any) => {
    viewVisible.value = true
    viewData.value = data
}
const handleSizeChange = (size: number) => {
    query.value.size = size
    buildTable()
}
const handleCurrentChange = (current: number) => {
    query.value.current = current
    buildTable()
}
onMounted(() => {
    buildTable()
})
const buildTable = async () => {
    const result = await logQuery(query.value)
    tableData.value = result.data.rows
    total.value = result.data.total
}

const formQuery = (reset: boolean) => {
    reset ? (query.value = initQuery()) : query.value
    buildTable()
}
</script>

<style scoped lang="scss">
.content-container {
    padding: 12px;
}
</style>